package zhuyun.code.swipe

import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
import android.view.View
import androidx.recyclerview.widget.RecyclerView

/**
 * @author: Administrator
 * @date: 2022/1/6
 */
class ItemDividerDecoration:RecyclerView.ItemDecoration() {
    private val mPaint = Paint(Paint.ANTI_ALIAS_FLAG)
    private val mDividerHeight = 1
    init {
        mPaint.color = Color.parseColor("#FF0000")
        mPaint.style = Paint.Style.FILL
    }
    override fun getItemOffsets(
        outRect: Rect,
        view: View,
        parent: RecyclerView,
        state: RecyclerView.State
    ) {
        outRect.set(0,0,0,mDividerHeight)
    }

    override fun onDraw(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        super.onDraw(c, parent, state)
        val left = parent.left + parent.paddingLeft
        val right = parent.right - parent.paddingRight
        val childCount = parent.childCount
        for(i in 0 until childCount){
            val child = parent.getChildAt(i)
            val top = child.bottom
            val bottom = top + mDividerHeight
            c.drawRect(left.toFloat(),top.toFloat(),right.toFloat(),bottom.toFloat(),mPaint)
        }

    }
}